package cn.afterturn.easypoi.test.excel.export;

import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.test.entity.MsgClient;
import cn.afterturn.easypoi.test.entity.MsgClientGroup;

/**
 * 大数据量导出 Created by JueYue on 2017/9/7.
 */
public class ExcelExportBigData {

	@Test
	public void bigDataExport() throws Exception {

		List<MsgClient> list = new ArrayList<MsgClient>();
		Workbook workbook = null;
		Date start = new Date();
		ExportParams params = new ExportParams("大数据测试", "测试");
		//start: ******************************************************************************/
		// 前置：excel 2003每个sheet在6万条以内;excel 2007每个sheet在100万条以内
		// 导出大数据量的一定要用2007及以后的版本
		// 因为xlsx一个sheet包含100万条记录，容量不过20M，如果用2003的xls则需要十几个sheet,容量超过80M，
		//end: ******************************************************************************/
		for (int i = 0; i < 600000; i++) {
			MsgClient client = new MsgClient();
			client.setBirthday(new Date());
			client.setClientName("小明" + i);
			client.setClientPhone("18797" + i);
			client.setCreateBy("JueYue");
			client.setId("1" + i);
			client.setRemark("测试" + i);
			MsgClientGroup group = new MsgClientGroup();
			group.setGroupName("测试" + i);
			client.setGroup(group);
			list.add(client);
			if (list.size() == 10000) {
				workbook = ExcelExportUtil.exportBigExcel07(params, MsgClient.class, list);
				list.clear();
			}
		}
		ExcelExportUtil.closeExportBigExcel07();
		System.out.println(new Date().getTime() - start.getTime());
		File savefile = new File("c:/excelllllllll/");
		if (!savefile.exists()) {
			savefile.mkdirs();
		}
		FileOutputStream fos = new FileOutputStream("c:/excelllllllll/ExcelExportBigData.bigDataExport.xlsx");
		workbook.write(fos);
		fos.close();
	}

}
